
#coding:utf-8
import wx,sys,os
import codecs
from Database import *
from sqlobject import *

class UserLoginDlg ( wx.Dialog  ):
    
    def __init__( self, parent, title = u"用户登录" ):
        wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = title, pos = wx.DefaultPosition, size = wx.Size( 240,150 ), style = wx.DEFAULT_DIALOG_STYLE ) 
        
        self.parent = parent
        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        self.SetBackgroundColour(wx.Colour(150, 200, 150 ))
        bSizer1 = wx.BoxSizer( wx.VERTICAL )
        
        bSizer2 = wx.BoxSizer( wx.HORIZONTAL )
        
        self.m_accText = wx.StaticText( self, wx.ID_ANY, u"用户名: ", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_accText.Wrap( -1 )
        self.m_accText.SetFont( wx.Font( 11, 74, 90, 90, False, "Tahoma" ) )
        
        bSizer2.Add( self.m_accText, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_accInputTextctrl = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.m_accInputTextctrl, 0, wx.ALIGN_BOTTOM|wx.ALL, 5 )
        
        
        bSizer1.Add( bSizer2, 0, wx.EXPAND, 5 )
        
        bSizer3 = wx.BoxSizer( wx.HORIZONTAL )
        
        self.m_pswdText = wx.StaticText( self, wx.ID_ANY, u"   密码: ", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_pswdText.Wrap( -1 )
        self.m_pswdText.SetFont( wx.Font( 11, 74, 90, 90, False, "Tahoma" ) )
        
        bSizer3.Add( self.m_pswdText, 0, wx.ALL|wx.ALIGN_BOTTOM, 5 )
        
        self.m_pswdTextctrl = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_PASSWORD )
        bSizer3.Add( self.m_pswdTextctrl, 0, wx.TOP|wx.RIGHT|wx.LEFT, 5 )
        
        
        bSizer1.Add( bSizer3, 0, wx.EXPAND, 5 )
        
        bSizer4 = wx.BoxSizer( wx.VERTICAL )
        
        #self.m_rmbLoginsts = wx.CheckBox( self, wx.ID_ANY, u"记住我的登录状态(1天)", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_rmdLoginMsg = wx.StaticText(self,wx.ID_ANY, u'',wx.DefaultPosition,wx.DefaultSize,0)
        bSizer4.Add( self.m_rmdLoginMsg, 1, wx.ALL|wx.EXPAND, 5 )
        
        
        bSizer1.Add( bSizer4, 0, wx.EXPAND, 5 )
        
        bSizer5 = wx.BoxSizer( wx.HORIZONTAL )
        
        self.m_loginBtn = wx.Button( self, wx.ID_OK, u"登录", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer5.AddSpacer((0,0),1,0,5)
        bSizer5.Add( self.m_loginBtn, 1, wx.ALL|wx.EXPAND, 5 )
        
        self.m_cancelBtn = wx.Button( self, wx.ID_CANCEL, u"取消", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer5.Add( self.m_cancelBtn, 1, wx.ALL|wx.EXPAND, 5 )
        
        #self.m_corrPswd = wx.StaticText( self, wx.ID_ANY, u"      ", wx.DefaultPosition, wx.DefaultSize, 0 )
        #self.m_corrPswd.Wrap( -1 )
        #self.m_corrPswd.SetFont( wx.Font( 9, 74, 90, 90, True, "Tahoma" ) )
        
        #bSizer5.Add( self.m_corrPswd, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        
        bSizer1.Add( bSizer5, 1, wx.EXPAND, 5 )
        
        
        self.SetSizer( bSizer1 )
        self.Layout()
        
        self.Centre( wx.BOTH )
        self.m_password = ''

        #if m_pswdTextctrl.IsEditable():
        self.Success = False
  
        
        self.m_loginBtn.Bind(wx.EVT_BUTTON, self.OnLoginBtn )
        self.m_cancelBtn.Bind(wx.EVT_BUTTON, self.OnCancelBtn )
        
        
    def OnLoginBtn(self, event):
        try:
            account = self.m_accInputTextctrl.GetValue()
            password = self.m_pswdTextctrl.GetValue()
        except:
            account = self.m_accInputTextctrl.GetLabel()
            password = self.m_pswdTextctrl.GetLabel()
        
        if account == u'admin' and password == u'admin':
            self.Success = True
            try:
                self.parent.m_userStatus = True
                self.parent.m_userExitItem.Enable(True)
            except:
                pass
            event.Skip()
            return
        
        q = MUser.select(MUser.q.name == account)
        
        ls = list(q)
#        print q,account,password,ls
        if len(ls) == 0:
            wx.MessageBox(u"该用户名不存在,请重新输入")
        elif ls[0].password != password:
            wx.MessageBox(u"用户密码不正确,请重新输入")
        else:
            #self.GetParent().self.m_userStatus = True
            #m_parent = self.GetParent()
            self.Success = True
            try:
                self.parent.m_userStatus = True
                self.parent.m_userExitItem.Enable(True)
            except:
                pass
            event.Skip()
        
    
    def OnCancelBtn(self, event):
        event.Skip()

if  __name__ == '__main__':
        Connect()
        app = wx.App()
        dlg = UserLoginDlg(None)
        dlg.Show()
        app.MainLoop()
